package com.jindu.producer.system.controller;

import com.jindu.kernel.page.AlphaPage;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.system.jpa.RolesJpa;
import com.jindu.producer.system.model.SRolesEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/1/28
 */
@Api(description = "角色信息管理", tags = "RolesController", basePath = "/role")
@RestController
@RequestMapping(value = "/role",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RolesController {

    Logger logger = LoggerFactory.getLogger(UsersController.class);

    @Autowired
    RolesJpa rolesJpa;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据角色ID，查询角色的详细信息", notes = "返回单个角色的信息,角色详细信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public JsonResult<SRolesEntity> get(@RequestParam String id){
        return JsonResult.getSuccessResult(rolesJpa.findById(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据分页条件，查询角色的信息列表", notes = "返回用户的信息列表,可以角色名称模糊查询")
    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public JsonResult<AlphaPage<SRolesEntity>> getAll(@RequestParam(value = "pageNo",defaultValue = "0") Integer pageNo,
                                                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                       @RequestParam(value = "name", required = false) String name){

        Specification querySpecifi = (Specification<SRolesEntity>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if(name != null){
                predicates.add(criteriaBuilder.like(root.get("name"),"%"+name+"%"));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Page<SRolesEntity> entities=rolesJpa.findAll(querySpecifi,new PageRequest(pageNo, pageSize));
        return new AlphaPage<>(entities).toJsonResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存角色信息", notes = "保存角色的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<SRolesEntity> save(@RequestBody SRolesEntity rolesEntity){

        SRolesEntity sRolesEntity=rolesJpa.findById(rolesEntity.getId());
        if(sRolesEntity==null){
            rolesEntity.setId(IdGenerator.uuid());
            rolesEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            rolesEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(rolesJpa.saveAndFlush(rolesEntity));
        }else {
            rolesEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(rolesEntity,sRolesEntity, AlphaBeanUtils.getNullPropertyNames(rolesEntity));
            return JsonResult.getSuccessResult(rolesJpa.saveAndFlush(sRolesEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除角色的信息", notes = "根据角色的ID,删除数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(rolesJpa.deleteById(id));
    }



}
